/* 
 *  <copyright file="AttachedRotationUpdater.cs" company="Wesley Jowitt">
 *  
 *  SobrietyEngine: Copyright (c) 2011 Wesley Jowitt
 *  
 *  This software is distributed under a software license.
 *   * As of the creation time of this file, the license in use is the Microsoft Permissive License (Ms-PL) 
 *    * The license can be viewed at:
 *     * License.Txt in the root folder of this project
 *      * http://sobrietyengine.codeplex.com/license
 *       * http://www.microsoft.com/opensource/licenses.mspx
 *       
 *  </copyright>
 *   */

using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.Xna.Framework;
using SobrietyEngine.Common;

using SobrietyEngine.Guids;
using SobrietyEngine.Actor;
using SobrietyEngine.Components.Attach;
using SobrietyEngine.Components.Common;

namespace SobrietyEngine.Components.Rotation
{
    public class AttachedRotationUpdater : AttachedToActor, SobrietyEngine.Components.IComponent
    {
        RotationCmp AttachedToRotation = null;
        RotationCmp MyRotation = null;

        float rotationOffset = 0.0f;
        public float RotationOffset { get { return rotationOffset; } set { rotationOffset = value; } }

        public AttachedRotationUpdater()
            : base()
        {
        }

        override public void Set(Parameters parms)
        {
            base.Set(parms);
            if (parms.ContainsKey("RotationOffset"))
                rotationOffset = (float)parms["RotationOffset"];
        }

        override public Boolean InitComponent()
        {
            base.InitComponent();

            AttachedToRotation = AttachedTo.Get<IRotation>();
            if (AttachedToRotation == null)
                throw new Exception("Attached to actor required to have a rotation!");

            MyRotation = ParentActor.Get<IRotation>();
            if (AttachedToRotation == null)
                throw new Exception("actor required to have a roation!");

            return true;
        }

        new public void Update(GameTime elapsedTime)
        {
            base.Update(elapsedTime);
            MyRotation.Set(AttachedToRotation.GetRadians() + rotationOffset);
        }
    }
}